using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Comteck.Winforms.Utils {
  /// <summary>
  /// 
  /// </summary>
  public class ResourcesHelper : BaseLog {

    /// <summary>
    /// 获取返回值
    /// </summary>
    /// <param name="dllName">dll名称</param>
    /// <param name="recouseName">资源名称</param>
    /// <param name="keyName">key名称</param>
    /// <returns></returns>
    public static string Load(string dllName, string recouseName, string keyName) {
      string key = null;
      Assembly assembly = null;
      ResourceManager resourceManager = null;

      if (!AppDomain.CurrentDomain.GetAssemblies().Exists(x => x.GetName().Name == dllName)) {
        Logger.Error($"程序集 {dllName} 不存在");
        return key;
      }

      try {


        assembly = Assembly.Load(dllName);
      } catch (Exception ex) {
        Logger.Error($"加载DLL {dllName} 失败", ex);
        return key;
      }

      try {
        resourceManager = new ResourceManager($"{dllName}.{recouseName}", assembly);
      } catch (Exception ex) {
        Logger.Error($"加载资源文件 {dllName}.{recouseName} 失败", ex);
        return key;
      }

      try {
        key = resourceManager.GetObject(keyName, System.Globalization.CultureInfo.CurrentCulture) as string;
      } catch (Exception ex) {
        Logger.Error($"获取 {dllName}.{recouseName}.{keyName} 失败", ex);
      } finally {
        resourceManager?.ReleaseAllResources();
      }

      return key;
    }

  }
}
